﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Collections.ObjectModel;
using System.Data;
using System.Windows.Media.Imaging;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows.Controls;
using System.Globalization;
using produccion.modelo;
using produccion.AccesoDatos;
using produccion.clases;
using produccion.forms;
using produccion.reportes;
using System.Windows.Input;
using System.Collections.Specialized;

namespace produccion.forms
{
    /// <summary>
    /// Lógica de interacción para FRMReportarProcesosConcluidos.xaml
    /// </summary>
    public partial class FRMReportarProcesosConcluidos : Window
    {
        public FRMReportarProcesosConcluidos()
        {
            InitializeComponent();
        }

        private void dtgProcesos_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter && elemento != null)
            {
                e.Handled = true;
                elemento.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
            }
        }

        //
        // SINGLE CLICK EDITING
        //
        private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DataGridCell cell = sender as DataGridCell;
            if (cell != null && !cell.IsEditing && !cell.IsReadOnly)
            {
                if (!cell.IsFocused)
                {
                    cell.Focus();
                }
                DataGrid dataGrid = FindVisualParent<DataGrid>(cell);
                if (dataGrid != null)
                {
                    if (dataGrid.SelectionUnit != DataGridSelectionUnit.FullRow)
                    {
                        if (!cell.IsSelected)
                            cell.IsSelected = true;
                    }
                    else
                    {
                        DataGridRow row = FindVisualParent<DataGridRow>(cell);
                        if (row != null && !row.IsSelected)
                        {
                            row.IsSelected = true;
                        }
                    }
                }
            }
        }

        static T FindVisualParent<T>(UIElement element) where T : UIElement
        {
            UIElement parent = element;
            while (parent != null)
            {
                T correctlyTyped = parent as T;
                if (correctlyTyped != null)
                {
                    return correctlyTyped;
                }

                parent = VisualTreeHelper.GetParent(parent) as UIElement;
            }
            return null;
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.F3 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta == true) { MDOgeneral.logBusquedadAbierta = false; }
            }

            if (e.Key == Key.F4 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta1 == true) { MDOgeneral.logBusquedadAbierta1 = false; }
            }

            if (e.Key == Key.F5 && elemento != null)
            {
                if (MDOgeneral.logBusquedadAbierta2 == true) { MDOgeneral.logBusquedadAbierta2 = false; }
            }
        }

        private void ProcesosConcluidos_Loaded(object sender, RoutedEventArgs e)
        {
            txtOrden.Style = null;
            //txtOperario.Style = null;
            //dtpFecha.Style = null;
            Keyboard.Focus(txtOrden);
        }

        private void txtOrden_TextChanged(object sender, TextChangedEventArgs e)
        {
            try
            {
                if (txtOrden.Text.Trim().Length == 6)
                {
                    MDOgeneral mod = new MDOgeneral();

                    MDOgeneral.spComando.CommandText = "SP_PASOSECC";
                    MDOgeneral.spComando.Connection = MDOgeneral.conexion;
                    MDOgeneral.spComando.CommandType = CommandType.StoredProcedure;
                    MDOgeneral.spComando.Parameters.Clear();

                    //Input
                    MDOgeneral.spComando.Parameters.Add("pva_nroord", Oracle.DataAccess.Client.OracleDbType.Varchar2, 40).Value = txtOrden.Text;
                    MDOgeneral.spComando.Parameters["pva_nroord"].Direction = ParameterDirection.Input;

                    //Output
                    MDOgeneral.spComando.Parameters.Add("pva_result", Oracle.DataAccess.Client.OracleDbType.Varchar2, 2000).Direction = ParameterDirection.Output;
                    MDOgeneral.spComando.Parameters.Add("pva_errusu", Oracle.DataAccess.Client.OracleDbType.Varchar2, 2000).Direction = ParameterDirection.Output;
                    MDOgeneral.spComando.ExecuteNonQuery();
                    string c = MDOgeneral.spComando.Parameters["pva_result"].Value.ToString();

                    DataTable dt = new DataTable();
                    dt = mod.consulta("select * from tmppasosecc");

                    foreach (DataRow dr in dt.Rows)
                    {
                        if (dr["LINEA_TMP"].ToString().Trim() == "Tallas")
                        {
                            dtgProcesos.Columns[2].Header = dr["T1_TMP"].ToString().Trim().Length>0 ? dr["T1_TMP"].ToString() : " ";
                            dtgProcesos.Columns[3].Header = dr["T2_TMP"].ToString().Trim().Length > 0 ? dr["T2_TMP"].ToString() : " ";
                            dtgProcesos.Columns[4].Header = dr["T3_TMP"].ToString().Trim().Length > 0 ? dr["T3_TMP"].ToString() : " ";
                            dtgProcesos.Columns[5].Header = dr["T4_TMP"].ToString().Trim().Length > 0 ? dr["T4_TMP"].ToString() : " ";
                            dtgProcesos.Columns[6].Header = dr["T5_TMP"].ToString().Trim().Length > 0 ? dr["T5_TMP"].ToString() : " ";
                            dtgProcesos.Columns[7].Header = dr["T6_TMP"].ToString().Trim().Length > 0 ? dr["T6_TMP"].ToString() : " ";
                            dtgProcesos.Columns[8].Header = dr["T7_TMP"].ToString().Trim().Length > 0 ? dr["T7_TMP"].ToString() : " ";
                            dtgProcesos.Columns[9].Header = dr["T8_TMP"].ToString().Trim().Length > 0 ? dr["T8_TMP"].ToString() : " ";
                            dtgProcesos.Columns[10].Header = dr["T9_TMP"].ToString().Trim().Length > 0 ? dr["T9_TMP"].ToString() : " ";
                            dtgProcesos.Columns[11].Header = dr["T10_TMP"].ToString().Trim().Length > 0 ? dr["T10_TMP"].ToString() : " ";
                        }
                    }
                }
            }
            catch (Exception ex) {MessageBox.Show(ex.Message);}
        }

        private void txtOperario_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter)
            {
                if (txtOperario.Text.Trim().Length == 5)
                    dtpFecha.Focus();
            }
        }


        private void btnGuardar_PreviewKeyUp(object sender, KeyEventArgs e)
        {
            txtOrden.Focus();
        }

        private void btnGuardar_Click(object sender, RoutedEventArgs e)
        {
            txtOrden.Focus();
        }

        private void txtOrden_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            var elemento = e.OriginalSource as UIElement;
            if (e.Key == Key.Enter)
            {
                if (txtOrden.Text.Trim().Length > 5)
                    txtOperario.Focus();
            }
        }

                
    }
}
